/**   
 * @Title: MD5.java 
 * @Package com.esun.util.md5 
 * @Description: (执行md5加密) 
 * @author Miko baoz@500wan.com   
 * @date 2011-1-19 上午11:33:38  
 */
package com.esun.util.md5;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @ClassName: MD5
 * @Description: TODO(用于执行Md5加密)
 * @author hxt
 * @date 2012-3-19 上午13:33:38
 * 
 */
public class MD5 {

	/**
	 * @Title: md5
	 * @Description: TODO(md5加密)
	 * @param str
	 *            需要加密的字符串
	 * @return String 加密后的字符串
	 */

	public static String md5(String s) {
		try {
			// Create MD5 Hash
			MessageDigest digest = java.security.MessageDigest
					.getInstance("MD5");
			digest.update(s.getBytes());
			byte messageDigest[] = digest.digest();

			// Create HEX String
			StringBuffer hexString = new StringBuffer();
			for (int i = 0; i < messageDigest.length; i++) {
				String shex = Integer.toHexString(0xFF & messageDigest[i]);
				if (shex.length() < 2) {
					shex = "0" + shex;
				}
				hexString.append(shex);
			}
			return hexString.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		return "";
	}
}
